<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Multi extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $category_id = $this->request->param('category_id');
        $where = [];
        if($category_id) {
            $where['category_id'] = $category_id;
        }
        $category = Db::name('category')->where(['type' => 'multi'])->select();
        $transport = Db::name('transport')->order('id desc')->select();
        $this->assign('category', $category);
        $this->assign('transport', $transport);
        $this->assign('title', '首页');
        return $this->view->fetch();
    }

    public function company()
    {
        $keywords = $this->request->param('keywords');
        $category_id = $this->request->param('category_id');
        $area_id = $this->request->param('area_id');
        $where = [];
        if($area_id) {
            $where['area_id'] = $area_id;
        }
        // else{
        //     $area_id = $this->request->param('area_id');
        //     if($area_id) {
        //         // $where['area_id'] = $area_id;
        //         $where = 'FIND_IN_SET('.$area_id.',area_id)';
        //     }
        // }

        $where['category_id'] = (int)$category_id;

        if(isset($keywords)) {
            $where['name'] = ['like', '%' . $keywords . '%'];
        }
        // halt($where);
        // $category = Db::name('category')->where(['id' => $category_id])->value('name');
        $list = Db::name('company')->where($where)->limit(10)->order('id desc')->select();
        $this->assign('list', $list);
        $this->assign('title', '多功能');
        return $this->view->fetch();
    }

    public function transport()
    {
        $where = [];
        $toarea = $this->request->param('toarea');
        if(isset($toarea)) {
            $area = Db::name('company_area')->where('name', 'like', '%' . $toarea . '%')->find();
            if($area) {
                $where = 'FIND_IN_SET('.$area['id'].',area_id)';
            }
        }
        $list = [];
        if($where) {
            $list = Db::name('transport')->where($where)->limit(20)->select();
        }
        $this->assign('list', $list);
        $this->assign('title', '物流公司');
        return $this->view->fetch();
    }

    public function company_info($id)
    {
        $info = Db::name('company')->where(['id' => $id])->find();
        if(!$info) {
            $this->error('页面不存在');
        }
        $info['banner'] = explode(',', $info['banner']);
        $this->assign('info', $info);
        $this->assign('title', '公司详情');
        return $this->view->fetch();
    }

    public function transport_info($id)
    {
        $info = Db::name('transport')->where(['id' => $id])->find();
        if(!$info) {
            $this->error('页面不存在');
        }
        $info['banner'] = explode(',', $info['banner']);
        $this->assign('info', $info);
        $this->assign('title', '公司详情');
        return $this->view->fetch('company_info');
    }

    public function area()
    {
        $category_id = $this->request->param('category_id');
        $area = Db::name('company_area')->select();
        if($area) {
            foreach ($area as $k => $v) {
                $area[$k]['category_id'] = $category_id;
            }
        }
        $this->assign('area', json_encode($area));
        $this->assign('title', '地区选择');
        return $this->view->fetch();
    }

}
